package com.lianniu.mgr.orderManage.controller;

import javax.servlet.http.HttpServletRequest;

import com.lianniu.mgr.baseService.dict.model.DictionaryVo;
import com.lianniu.mgr.baseService.dict.service.IDictionaryService;
import com.lianniu.mgr.baseService.staff.service.IStaffService;
import com.lianniu.mgr.orderManage.vo.OrderDetailVo;
import com.lianniu.mgr.orderManage.vo.OrderInfoVo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.orderManage.service.IOrderManageService;
import com.lianniu.mgr.orderManage.vo.OrderConditionVo;
import com.lianniu.mgr.orderManage.vo.ShipmentVo;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.ObjectUtil;

import java.util.List;

@Controller
@RequestMapping("/rest/orderManage")
public class OrderManageRestController {
    
    private static final Logger logger = Logger.getLogger(OrderManageController.class);

  @Autowired
  IOrderManageService orderManageService;
  @Autowired
  IDictionaryService dictionaryService;
  @Autowired
  IStaffService staffService;
  @RequestMapping(value="/orderList",method={RequestMethod.GET,RequestMethod.POST})
  public @ResponseBody BaseResult getOrderList(HttpServletRequest req,OrderConditionVo condition,PaginationInfo paginationInfo){
      logger.info("getOrderList begin");
      String orderCode = req.getParameter("orderCode");
      String orderStatus = req.getParameter("orderStatus");
      String productName = req.getParameter("productName");
      String buyerName = req.getParameter("buyerName");
      String addTimeFrom = req.getParameter("addTimeFrom");
      String addTimeTo = req.getParameter("addTimeTo");
      BaseResult orders = null;
      if(!"".equals(orderCode) || !"".equals(orderStatus)||!"".equals(productName)||!"".equals(buyerName)||!"".equals(addTimeFrom)||!"".equals(addTimeTo)){
          condition.setOrderCode(orderCode);
          condition.setOrderStatus(orderStatus);
          condition.setProductName(productName);
          condition.setBuyerName(buyerName);
          condition.setAddTimeFrom(addTimeFrom);
          condition.setAddTimeTo(addTimeTo);
      }
      logger.info("data:"+condition);
      orders = orderManageService.getOrderListForPage(condition, paginationInfo);
      logger.info("params[订单数据]"+JSON.toJSONString(orders));
    return orders;
  }
  
  @RequestMapping("/getOrder")
  public @ResponseBody BaseResult getOrder(String orderId,ModelMap map){
    BaseResult order = orderManageService.getOrderById(orderId);
    DictionaryVo dic = new DictionaryVo();
    dic.setTypeName("ApproveStatus");
    BaseResult approveStatus= dictionaryService.selectDictionaryByDicType(dic);
    map.put("approveStatus", approveStatus.getData());
    ObjectUtil.nvlProperty(order.getData());
    map.put("order", order);
    BaseResult staffPostion= staffService.getStaffByPostion("业务员");
    map.put("staff",staffPostion);
    return BaseResult.success(map);
  }
  @RequestMapping("/shipment")
  public @ResponseBody BaseResult shipment(ShipmentVo shipment){
    return orderManageService.shipment(shipment);
  }

   @RequestMapping("/delOrder")
   public @ResponseBody BaseResult delOrder( String orderId){
        logger.info(String.format("delOrder %s OrderInfo", orderId));
        BaseResult result = orderManageService.updateOrderDelFlag(orderId);
        logger.info("delOrder buyer result ： "+result);
        return result;
   }

    @RequestMapping(value="/modifyOrder",method={RequestMethod.POST})
    public @ResponseBody BaseResult modifyOrder(HttpServletRequest req,OrderInfoVo orderVo){
        //处理订单数据
        String orderDetailId=req.getParameter("orderDetailId");
        String actProductNum = req.getParameter("actProductNum_".concat(orderDetailId.toString()));
        String actPrice = req.getParameter("actPrice_"+orderDetailId.toString());

        OrderDetailVo curOrderDetail=new OrderDetailVo();
        curOrderDetail.setId(orderDetailId);
        curOrderDetail.setActProductNum(actProductNum);
        curOrderDetail.setActPrice(actPrice);
        float curPrice=Float.parseFloat(actPrice)*Float.parseFloat(actProductNum);
        curOrderDetail.setPrice(String.valueOf(curPrice));
        orderVo.setPrice(String.valueOf(curPrice));
        orderVo.setRecvPrice(String.valueOf(curPrice+Float.parseFloat(orderVo.getSeriviceFee())));

        orderManageService.modifyOrder(orderVo,curOrderDetail);
        return BaseResult.success();
    }
    @RequestMapping("/modifyStaff")
    public @ResponseBody BaseResult modifyStaff(String  orderId,String staffModify){
        orderManageService.modifyStaff(orderId,staffModify);
        return BaseResult.success();
    }

    @RequestMapping("/modifyComment")
    public @ResponseBody BaseResult modifyComment(String  orderId,String commResult,String comment){
        orderManageService.modifyComment(orderId,commResult,comment);
        return BaseResult.success();
    }

    @RequestMapping("/findOrder")
    public @ResponseBody BaseResult findOrder( String orderId){
        logger.info(String.format("findOrder %s OrderInfo", orderId));
        String status="02";
        BaseResult result = orderManageService.updateOrderStatus(orderId,status);
        logger.info("findOrder buyer result ： "+result);
        return result;
    }

    @RequestMapping("/cancleOrder")
    public @ResponseBody BaseResult cancleOrder( String orderId,String cancelReason){
        logger.info(String.format("cancleOrder %s OrderInfo", orderId));
        String status="08";
        BaseResult result = orderManageService.updateOrderStatus(orderId,status,cancelReason);
        logger.info("cancleOrder buyer result ： "+result);
        return result;
    }

    @RequestMapping("/modifySF")
    public @ResponseBody BaseResult modifySF(String orderId,String searchFinishedRemark,String searchFinishedImg1,String searchFinishedImg2){
        logger.info(String.format("modifySF %s orderId", orderId));
        String status="03";
        BaseResult result = orderManageService.updateOrderSF(orderId,status,searchFinishedRemark,searchFinishedImg1,searchFinishedImg2);
        logger.info("modifySF orderId result ： "+result);
        return result;
    }

    @RequestMapping("/modifyFA")
    public @ResponseBody BaseResult modifyFA(String orderId,String paidRemark,String paidImgUrl1,String paidImgUrl2){
        logger.info(String.format("modifyFA %s orderId", orderId));
        String status="04";
        BaseResult result = orderManageService.updateOrderFA(orderId,status,paidRemark,paidImgUrl1,paidImgUrl2);
        logger.info("modifyFA orderId result ： "+result);
        return result;
    }

    @RequestMapping("/modifyDL")
    public @ResponseBody BaseResult modifyDL(String orderId,String deliveRemark,String deliveImgUrl1,String deliveImgUrl2){
        logger.info(String.format("modifyDL %s orderId", orderId));
        String status="05";
        BaseResult result = orderManageService.updateOrderDL(orderId,status,deliveRemark,deliveImgUrl1,deliveImgUrl2);
        logger.info("modifyDL orderId result ： "+result);
        return result;
    }

    @RequestMapping("/modifyCF")
    public @ResponseBody BaseResult modifyCF(String orderId,String confirmRemark,String confirmImg1,String confirmImg2){
        logger.info(String.format("modifyCF %s orderId", orderId));
        String status="06";
        BaseResult result = orderManageService.updateOrderCF(orderId,status,confirmRemark,confirmImg1,confirmImg2);
        logger.info("modifyCF orderId result ： "+result);
        return result;
    }

}
